package com.toogu.mazhangmen.ui.login;

import android.app.Activity;
import android.util.Log;

import com.google.gson.Gson;
import com.toogu.mazhangmen.bean.ErrorBean;
import com.toogu.mazhangmen.util.NetWorkUtil;
import com.toogu.mazhangmen.util.TokenUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginModel {
    private LoginPresenter presenter;
    private Activity activity;

    interface OnResult{
        void onSuccess(String responseText);
    }
    class MyCallback implements Callback {
        private OnResult onResult;
        public MyCallback(OnResult onResult){
            this.onResult=onResult;
        }
        @Override
        public void onFailure(Call call, IOException e) {
            presenter.onLoginFail(e.getMessage());
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String responseText = response.body().string();
            if(response.isSuccessful()){
                onResult.onSuccess(responseText);
            }
            else{
                ErrorBean errorBean = new Gson().fromJson(responseText, ErrorBean.class);
                presenter.onLoginFail(errorBean.getMessage());
            }
        }
    }

    public LoginModel(Activity activity, LoginPresenter presenter){
        this.presenter=presenter;
        this.activity=activity;
    }

    public void login(String userName, String password) {
        if(userName.isEmpty()||password.isEmpty()){
            presenter.onLoginFail("账号密码不能为空");
        }
        else{
            NetWorkUtil.getNetWorkUtil().AuthToken(activity,userName,password, new MyCallback(new OnResult() {
                @Override
                public void onSuccess(String responseText) {

                    TokenUtil tokenUtil = new Gson().fromJson(responseText, TokenUtil.class);
                    tokenUtil.setToken();
                    NetWorkUtil.getNetWorkUtil().getUserInfo(activity, new MyCallback(new OnResult() {
                        @Override
                        public void onSuccess(String responseText) {
                            TokenUtil tokenUtil = new Gson().fromJson(responseText, TokenUtil.class);
                            Log.d("TokenUtil", "onSuccess: "+responseText);
                            tokenUtil.setUserBean(userName,password);
                            presenter.onLoginSuccess();
//                            EquipmentUtil.getInstance().getNewData(activity,new EquipmentUtil.ProjectCallback() {
//                                @Override
//                                public void onGetProjectSuccess() {
//                                    presenter.onLoginSuccess();
//                                }
//                                @Override
//                                public void onGetProjectFailed() {
//                                    presenter.onLoginFail("获取设备信息失败");
//                                }
//                            });
                        }
                    }));
                }
            }));
        }
    }
}
